package array;

public class L121 {
    public int maxProfit(int[] prices) {
      /*  int max=0;
        for (int i = 0; i < prices.length; i++) {
            int current=prices[i];
            for (int j = i+1; j < prices.length ; j++) {
                if (current<prices[j]){
                    max=(prices[j]-current)>max ?(prices[j]-current):max;
                }

            }
        }

        return max;*/
        if (prices == null || prices.length == 0) {
            return 0;
        }

        int minPrice = Integer.MAX_VALUE;
        int maxProfit = 0;
        for (int price : prices) {
            if (price<minPrice){
                minPrice=price;
            }
            //计算当前利润并更新最大收益
            int profit=price-minPrice;
            maxProfit=profit>maxProfit ? profit:maxProfit;

        }
        return maxProfit;

    }
}
